package shopping.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shopping.demo.cache.OrderCache;
import shopping.demo.dao.OrderDao;
import shopping.demo.entities.Order;
import shopping.demo.exception.NotExistException;
import shopping.demo.exception.order.OrderAlreadyExistException;
import shopping.demo.exception.order.OrderNotExistException;
import shopping.demo.util.OrderIdUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderCache orderCache;

    private Logger logger = LoggerFactory.getLogger(Logger.class);

//    @PostConstruct
//    public void init() {
//        for(Order order:orderDao.findAll()) {
//            orderCache.setOrderCache(order);
//        }
//        logger.info("Order预热完毕");
//    }

    public List<Order> findAll() {
        List<Order> orders = orderCache.getOrders();
        return orders.size() == 0?orderDao.findAll():orders;
    }

    public Order findById(String oid) {
        Order order = orderCache.getCache(oid);
        //缓存未命中
        if(order == null) {
            //从数据库进行查询
            Order new_order = orderDao.findById(oid);
            //更新缓存
            orderCache.setOrderCache(new_order);
            return new_order;
        }
        return order;
    }

//    public List<Order> findByUserId(Long uid) {
//        List<Order> orders = orderCache.getCache(uid);
//        return orders.size() == 0?orderDao.findByUserId(uid):orders;
//    }

    @Transactional
    public void deleteById(String oid) throws Exception{
        // 删除缓存部分
        // orderCache.deleteOrderCache(id);
        int count = orderDao.deleteById(oid);
        if(count == 0) {
            throw new NotExistException("订单");
        }
        logger.info("删除了订单，订单号为：{}",oid);
    }

    @Transactional
    public void changeStatus(String oid,Integer status){
        if(orderDao.findById(oid) == null) {
            throw new OrderNotExistException();
        }
        orderDao.changeStatus(oid,status);
        logger.info("订单【{}】状态 -> {}",oid,status);
    }

    //创建订单
    @Transactional
    public Order createOrder(Long uid, Integer gid, Integer count,Integer price) throws Exception{
        DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String time = dateFormat.format(new Date());
        String oid = OrderIdUtils.getOrderId(uid,gid);
        if(orderDao.findById(oid) != null) {
            throw new OrderAlreadyExistException();
        }
        Order order = new Order(oid,time,uid,gid,count,price,OrderIdUtils.CREATED);

        orderDao.createOrder(order.getOid(),order.getTime(),order.getUid(),order.getGid(),order.getCount(),order.getPrice(),order.getStatus());
        return order;
    }


}